const mysql = require('mysql2/promise');

async function testConnection() {
  console.log('尝试连接到数据库...');
  const connectionUrl = 'mysql://root:zZ20021022@localhost:3306/supermarket_management';
  console.log('连接URL:', connectionUrl);

  try {
    const connection = await mysql.createConnection(connectionUrl);
    console.log('数据库连接成功');
    
    // 检查数据库是否存在
    const [rows] = await connection.query('SHOW DATABASES');
    console.log('可用数据库:');
    rows.forEach(row => console.log(row.Database));
    
    // 检查表是否存在
    const [tables] = await connection.query('SHOW TABLES');
    console.log('\n数据库表:');
    tables.forEach(table => console.log(table.Tables_in_supermarket_management));
    
    await connection.end();
  } catch (err) {
    console.error('数据库连接错误:');
    console.error('错误代码:', err.code);
    console.error('错误信息:', err.message);
    console.error('SQL状态:', err.sqlState);
    console.error('完整错误:', err);
  }
}

testConnection(); 